package top.fkxuexi.smartwork.core;


import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.Version;
import top.fkxuexi.smartwork.CodeGener;
import top.fkxuexi.smartwork.MetaEntity.TableMeta;
import top.fkxuexi.smartwork.config.GenerConfig;
import top.fkxuexi.smartwork.config.SmkConfig;
import top.fkxuexi.smartwork.tools.FileUtils;

import javax.annotation.Resource;
import java.io.EOFException;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class MakeCode {

    private SmkConfig config;
    private List<TableMeta> tableMetaList;

    public MakeCode(SmkConfig config, List<TableMeta> tableMetaList) {
        if (tableMetaList == null || tableMetaList.size() == 0) {
            throw new RuntimeException("数据库元数据是不能为空滴哟………………");
        }
        this.config = config;
        this.tableMetaList = tableMetaList;
    }


    /**
     * 依据模板来生成代码
     * @throws IOException
     * @throws TemplateException
     */
    public void makeCode() throws IOException, TemplateException {
        Configuration cfg = new Configuration(new Version("2.3.28"));
        Template template;
        FileWriter fileWriter;
        Map<String, Object> map;
        for (TableMeta tableMeta : tableMetaList) {

            GenerConfig[] generConfigs = config.getGenerConfig();
            if (generConfigs == null || generConfigs.length == 0){
                throw new RuntimeException("生成的模板配置不能为空；"+"config.getGenerConfig() 为空");
            }
            for (GenerConfig generConfig : generConfigs) {
                template = getTemplate(generConfig,cfg);
                fileWriter = FileUtils.getFileWriter(generConfig, config, tableMeta.getJavaDbName());
                map = new HashMap<>();
                map.put("meta", tableMeta);
                map.put("config", config);
                map.put("generCfg", generConfig);
                template.process(map, fileWriter);
            }
        }

    }


    private Template getTemplate(GenerConfig conf , Configuration cfg) throws IOException {
        // 首先先卸载，之前所指定的模板，以防止用户在自定义模板是产生影响
        cfg.unsetTemplateLoader();
        try{
            URL resource = MakeCode.class.getResource(conf.getTemplateLocation());
            if (resource == null){
                resource = MakeCode.class.getClassLoader().getResource(conf.getTemplateLocation());
            }
            File file = new File(resource.getPath());
            cfg.setDirectoryForTemplateLoading(file.getParentFile());
            Template template = cfg.getTemplate(file.getName());
            return template;
        }catch (Exception e){
            // 如果是使用的是内置的模板的话，就会使用这个来加载
            TemplateLoader templateLoader = new FreemarkerTemplateLoader(cfg);
            cfg.setTemplateLoader(templateLoader);
            Template template = cfg.getTemplate(conf.getTemplateLocation());
            return template;
        }
    }

}
